import axios from "axios";
import {ElMessage} from 'element-plus'

const service = axios.create(
    {
        baseURL: "http://127.0.0.1:8081",
    }
)
service.defaults.headers.post['Content-Type'] = 'application/json';


const statusCodeMap = new Map();
statusCodeMap.set(404, "未知的请求");
statusCodeMap.set(500, "服务器异常");
const ignoreCode = [501];


service.interceptors.response.use(
    res => {
        const {code, message} = res.data;
        if (code !== 200) {
            if (ignoreCode.includes(code)) {
                return res;
            }
            ElMessage({
                message: `请求错误,接口路径:${res?.request?.responseURL},错误信息:${message}`,
                type: "warning"
            })
        }
        return res;
    },
    error => {
        ElMessage.error({
            message: `请求异常${error.message}`,
            type: "error"
        })

    }
)
export default service;